﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
    /// <summary>
    /// UI和指针的位置偏移量
    /// </summary>
    Vector3 offset;
    RectTransform rt;
    Vector3 pos;
    float minWidth; //水平最小拖拽范围
    float maxWidth; //水平最大拖拽范围
    float minHeight; //垂直最小拖拽范围  
    float maxHeight; //垂直最大拖拽范围
    float rangeX; //拖拽范围
    float rangeY; //拖拽范围
    protected virtual void initData () {
        rt = GetComponent<RectTransform> ();
        pos = rt.position;
        minWidth = rt.rect.width / 2;
        maxWidth = Screen.width - (rt.rect.width / 2);
        minHeight = rt.rect.height / 2;
        maxHeight = Screen.height - (rt.rect.height / 2);
    }
    /// <summary>
    /// 开始拖拽
    /// </summary>
    public virtual void OnBeginDrag (PointerEventData eventData) {
        Vector3 globalMousePos;
        //将屏幕坐标转换成世界坐标
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle (rt, eventData.position, null, out globalMousePos)) {
            //计算UI和指针之间的位置偏移量
            offset = rt.position - globalMousePos;
        }

    }
    /// <summary>
    /// 拖拽中
    /// </summary>
    public virtual void OnDrag (PointerEventData eventData) {
        SetDraggedPosition (eventData);
    }
    /// <summary>
    /// 结束拖拽
    /// </summary>
    public virtual void OnEndDrag (PointerEventData eventData) {

    }
    /// <summary>
    /// 更新UI的位置
    /// </summary>
    private void SetDraggedPosition (PointerEventData eventData) {
        Vector3 globalMousePos;

        if (RectTransformUtility.ScreenPointToWorldPointInRectangle (rt, eventData.position, null, out globalMousePos)) {
            Vector3 toP = DragRangeLimit (offset + globalMousePos);
            rt.position = toP;
        }
    }
    /// <summary>
    /// 拖拽范围限制
    /// </summary>
    Vector3 DragRangeLimit (Vector3 toP) {
        //限制水平/垂直拖拽范围在最小/最大值内
        rangeX = Mathf.Clamp (toP.x, minWidth, maxWidth);
        rangeY = Mathf.Clamp (toP.y, minHeight, maxHeight);
        //更新位置
        Vector3 _toP = new Vector3 (rangeX, rangeY, 0);
        return _toP;
    }
}